FROM alpine:3.8
LABEL Maintainer="yiyu <yiyu@newlink.com>" \
      Description="FPM7.2 based on Alpine"

# env list
ENV TIMEZONE=Asia/Shanghai \
    CPHALCON_VERSION=3.4.5 \
    PHPREDIS_VERSION=4.3.0 \
    COMPOSER_VERSION=1.10.20 \
    SWOOLE_VERSION=4.4.2 \
    MONGODB_VERSION=1.7.4 \
    PROBUF_VERSION=3.17.1 \
    REDIS_VERSION=5.1.1 \
    XDEBUG_VERSION=2.6.1 \
    TIDEWAYS_VERSION=4.1.6 \
    MEMCACHED_VERSION=3.1.3 \
    LANG=en_US.UTF-8 \
    LANGUAGE=en_US.UTF-8

# work dir
WORKDIR /var/www/app

COPY docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh

# copy composer
COPY php/package/composer_${COMPOSER_VERSION}.phar /usr/local/bin/composer

# phalcon source package | notice:::source.*.bash2sh
ADD php/package/cphalcon-${CPHALCON_VERSION}.tar.gz /usr/local/src/

# tideways source package | notice:::source.*.bash2sh
ADD php/package/tideways-${TIDEWAYS_VERSION}.tar.gz /usr/local/src/

# xdebug source package | notice:::source.*.bash2sh
ADD php/package/xdebug-${XDEBUG_VERSION}.tgz /usr/local/src/

# mongodb source package | notice:::source.*.bash2sh
ADD php/package/mongodb-${MONGODB_VERSION}.tgz /usr/local/src/

# xhgui source package | notice:::source.*.bash2sh
ADD php/package/xhgui.tar.gz /var/www/

# install dependent app including php
RUN set -xue && \
    sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories && \
    # install evn dependence
    apk --no-cache add --virtual .build-deps --update \
        tzdata \
        gcc \
        g++ \
        vim \
        libc-dev \
        make \
        automake \
        autoconf \
        libressl \
        libressl-dev \
        libzip-dev \
        pcre-dev \
        zlib-dev \
        linux-headers \
        curl \
        wget \
        curl-dev \
        gnupg \
        libxslt-dev \
        geoip-dev \
        pcre-dev \
        zlib-dev \
        freetype \
        libpng \
        libjpeg-turbo \
        freetype-dev \
        libpng-dev \
        libjpeg-turbo-dev \
        gd-dev \
        gd \
        librdkafka-dev \
        php7-dev \
        php7-pear \
    && apk --no-cache add \
        php7 \
        php7-fpm \
        php7-pdo \
        php7-pdo_mysql \
        php-mysqlnd \
        php7-mysqli \
        php7-json \
        php7-openssl \
        php7-curl \
        php7-zlib \
        php7-xml \
        php7-phar \
        php7-intl \
        php7-pcntl \
        php7-dom \
        php7-exif \
        php7-fileinfo \
        php7-calendar \
        php7-bz2 \
        php7-ctype \
        php7-session \
        php7-mbstring \
        php7-gd \
        php7-pdo \
        php7-opcache \
        php7-posix \
        php7-bcmath \
        php7-pdo_pgsql \
        php7-ftp \
        php7-tokenizer \
        php7-xmlwriter \
        php7-xsl \
        php7-xml \
        php7-zip \
        php7-xmlrpc \
        php7-xmlreader \
        php7-simplexml \
        php7-iconv \
        php7-sockets \
        php7-sysvmsg \
        php7-sysvsem \
        php7-sysvshm \
        php7-tidy \
        php7-soap \
        nginx \
        nginx-mod-http-perl \
        redis \
    # install cphalcon by source build
    && cd /usr/local/src/cphalcon-${CPHALCON_VERSION}/build \
    && ./install \
    && echo extension=phalcon.so >> /etc/php7/conf.d/phalcon.ini \
    # install igbinary by pecl.online
    && pecl install igbinary \
    && echo extension=igbinary.so >> /etc/php7/conf.d/igbinary.ini \
    # install protobuf by pecl.online
    && pecl install protobuf-${PROBUF_VERSION} \
    && echo extension=protobuf.so >> /etc/php7/conf.d/protobuf.ini \
    # install swoole by pecl.online
    && pecl install swoole-${SWOOLE_VERSION} \
    && echo extension=swoole.so >> /etc/php7/conf.d/swoole.ini \
    # install redis by pecl.online
    && pecl install redis-${REDIS_VERSION} \
    && echo extension=redis.so >> /etc/php7/conf.d/redis.ini \
    # install xdebug by source build
    && cd /usr/local/src/xdebug-${XDEBUG_VERSION} \
    && phpize && ./configure \
    && make && make install \
    && echo zend_extension=xdebug.so >> /etc/php7/conf.d/xdebug.ini \
    # install mongodb by source build
    && cd /usr/local/src/mongodb-${MONGODB_VERSION} \
    && phpize && ./configure \
    && make && make install \
    && echo extension=mongodb.so >> /etc/php7/conf.d/mongodb.ini \
    # install tideways by source build
    && cd /usr/local/src/tideways-${TIDEWAYS_VERSION} \
    && phpize && ./configure \
    && make && make install \
    && echo extension=tideways.so >> /etc/php7/conf.d/tideways.ini \
    && chmod 755 /usr/lib/php7/modules/*.so \
    # timezone
    && ln -snf /usr/share/zoneinfo/${TIMEZONE} /etc/localtime \
    && echo $TIMEZONE > /etc/timezone \
    # create www.www and configuration for application
    && addgroup -S www \
    && adduser -D -S -h /var/cache/www -s /sbin/nologin -G www www \
    && chown -R www.www /run /var/log/php7 \
    && chown -R www.www /run /var/lib/nginx /var/tmp/nginx /var/www/ /var/log  \
    && cd /run && mkdir nginx && cd /run/nginx && touch nginx.pid \
    # clean
    && apk del .build-deps -f \
    && rm -rf /tmp/* /var/www/localhost /var/cache/apk/* /usr/local/src/ \

# configure php-fpm
COPY php/php-fpm.d/www.conf /etc/php7/php-fpm.d/www.conf
COPY php/php.ini /etc/php7/php.ini

# configure nginx
COPY nginx/conf.d/ /etc/nginx/conf.d/
COPY nginx/nginx.conf /etc/nginx/nginx.conf

# configure redis
COPY redis/redis.conf /etc/redis/redis.conf

# 安装xhgui
RUN cd /var/www/xhgui \
    && php install.php


# expose port
EXPOSE 9000 80 443 6379

# docker start container command
CMD ["docker-entrypoint.sh"]
